export class DateTime {
  constructor () {
    this.defaultDateFormat = 'Y-m-d H:i:s'
    this.defaultTimezone = 8
    this.setTimeZone(this.defaultTimezone)
  }

  setTimeZone (timezone) {
    if (timezone) {
      this.timezone = parseInt(timezone)
    }
    return this
  }

  getDateObj (time) {
    return time ? new Date(time) : new Date()
  }

  getTime (datetime, showSenconds) {
    let time = this.getDateObj(datetime).getTime()
    if (showSenconds) {
      time = Math.trunc(time / 1000)
    }
    return time
  }

  getDate (dateFormat, time) {
    return this.dateFormat(dateFormat, time)
  }

  getTimeByTimeZone (time, timezone) {
    this.setTimeZone(timezone)
  }

  getTimeInfo (time, full = true) {
    const thisDate = this.getDateObj()
    const localTime = thisDate.getTime()
    const offset = thisDate.getTimezoneOffset()
    const thisTime = time || localTime
    const utc = offset * 60000 + thisTime
    const sqTime = utc + (3600000 * this.timezone)
    // console.log("sqTime", sqTime)
    const date = this.getDateObj(sqTime)
    const retData = {
      nYear: date.getFullYear(),
      nMonth: date.getMonth() + 1,
      nWeek: date.getDay() || 7,
      nDay: date.getDate(),
      nHour: date.getHours(),
      nMinutes: date.getMinutes(),
      nSeconds: date.getSeconds()
    }

    if (full) {
      for (const k in retData) {
        if (retData[k] < 10) {
          retData[k] = '0' + retData[k]
        }
      }
    }
    return retData
  }

  dateFormat (format, time) {
    const timeInfo = this.getTimeInfo(time)
    format = format || this.defaultDateFormat
    let date = format
    if (format) {
      date = date.replace(/Y/, timeInfo.nYear)
    }
    if (format.indexOf('m') !== false) {
      date = date.replace(/m/, timeInfo.nMonth)
    }
    if (format.indexOf('d') !== false) {
      date = date.replace(/d/, timeInfo.nDay)
    }
    if (format.indexOf('H') !== false) {
      date = date.replace(/H/, timeInfo.nHour)
    }
    if (format.indexOf('i') !== false) {
      date = date.replace(/i/, timeInfo.nMinutes)
    }
    if (format.indexOf('s') !== false) {
      date = date.replace(/s/, timeInfo.nSeconds)
    }
    return date
  }

  getUTC (datetime) {
    return this.getDateObj(datetime).toUTCString()
  }

  getISO (datetime) {
    return this.getDateObj(datetime).toISOString()
  }

  // 获取两时间相差天数
  getDiffDays (time1, time2) {
    if (!time1) {
      return false
    }
    time2 = time2 ?? this.getTime()

    let diffTime = time2 - time1
    if (diffTime < 0) {
      diffTime = Math.abs(diffTime)
    }

    return Math.ceil(diffTime / 86400000)
  }

  // 字符串转时间戳
  strToTime (str) {
    if(Array.from(str).length === 10) {
      str += '000'
    }
    return this.getTime(parseInt(str))
  }


  // 根据设置的天数获取时间戳
  getTimeBySetDays (days, time, getAll = false) {
    const date = this.getDateObj(time)
    date.setDate(date.getDate() + days)
    if (!getAll) {
      date.setHours(0)
      date.setMinutes(0)
      date.setSeconds(0)
      date.setMilliseconds(0)
    }
    return date.getTime()
  }

  // 根据设置的小时数获取天数
  getTimeBySetHours (hours, time, getAll = false) {
    const date = this.getDateObj(time)
    date.setHours(date.getHours() + hours)
    if (!getAll) {
      date.setMinutes(0)
      date.setSeconds(0)
      date.setMilliseconds(0)
    }
    return date.getTime()
  }

  //根据类型获取时间范围
  getTimeDimensionByType(type, offset = 0, thistime,  getAll = false) {
      let date = this.getDateObj(thistime)
      let startTime = 0
      let endTime = 0
      switch(type) {
        case 'hour':
          date.setHours(date.getHours() + offset)
          if (!getAll) {
            date.setMinutes(0)
            date.setSeconds(0)
            date.setMilliseconds(0)
          }
          startTime = date.getTime()
          endTime = getAll ? startTime : startTime + 3599999
          break
        case 'day':
          date.setDate(date.getDate() + offset)
          if (!getAll) {
            date.setHours(0)
            date.setMinutes(0)
            date.setSeconds(0)
            date.setMilliseconds(0)
          }
          startTime = date.getTime()
          endTime = getAll ? startTime : startTime + 86399999
          break
        case 'week':
          let day = date.getDay() || 7
          let offsetDays = 1 - day   
          if(offset) {
            offset = offset * 7 + offsetDays
          }
          date.setDate(date.getDate() + offset)
          if (!getAll) {
            date.setHours(0)
            date.setMinutes(0)
            date.setSeconds(0)
            date.setMilliseconds(0)
          }
          startTime = date.getTime()
          endTime = getAll ? startTime + 86400000 * 6 : startTime + 86400000 * 6 + 86399999
          break
        case 'month':
          date.setMonth(date.getMonth() + offset)
          if (!getAll) {
            date.setDate(1)
            date.setHours(0)
            date.setMinutes(0)
            date.setSeconds(0)
            date.setMilliseconds(0)
          }
          startTime = date.getTime()
          let nextMonthFirstDayTime = new Date(date.getFullYear(), date.getMonth() + 1, 1).getTime()
          endTime = getAll ? nextMonthFirstDayTime - 86400 : nextMonthFirstDayTime - 1
          break        
      }

      return {
        startTime,
        endTime
      }
  }
}
